import pandas as pd
import numpy as np

# 一、创建 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# 数组创建 默认从0开始
# a = [1, 2, 3]
# myvar = pd.Series(a)
# print(myvar)
# print(myvar[1])

# 数组创建 指定了 index
# a = ["Google", "Runoob", "Wiki"]
# myvar = pd.Series(a, index=["x", "y", "z"])
# print(myvar)
# print(myvar["y"])

# 字典
# sites = {1: "Google", 2: "Runoob", 3: "Wiki"}
# myvar = pd.Series(sites)
# print(myvar)

# 只需要字典中的一部分数据，指定需要数据的索引
# myvar = pd.Series(sites, index=[1, 2])
# print(myvar)

# name 设置名字
# sites = {1: "Google", 2: "Runoob", 3: "Wiki"}
# myvar = pd.Series(sites, index=[1, 2], name="RUNOOB-Series-TEST" )
# print(myvar)

# 二、基本操作 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a = [0, 4, 10, 2, 1]
b  = ["Google", "Runoob", "Wiki", "Test", "Python"]
series = pd.Series(a)
series_with_index = pd.Series(b, index=["x", "y", "z", "a", "b"])
print("一维数组: ")
print(series)

# 获取值
value = series[2]  # 获取索引为2的值
print("获取索引为2的值: "+str(value))

# 获取多个值
subset = series[1:4]  # 获取索引为1到3的值
print("获取索引为1到3的值: ")
print(subset)

# 使用自定义索引
value = series_with_index['b']  # 获取索引为'b'的值
print("获取索引为b的值: "+value)

# 索引和值的对应关系
for index, value in series_with_index.items():
    print(f"Index: {index}, Value: {value}")

# 三、属性和方法 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# 获取索引
index = series_with_index.index
print("索引: ")
print(index)

# 获取值数组
values = series_with_index.values
print("值数组: ")
print(values)

# 获取描述统计信息
stats = series_with_index.describe()
print("描述统计信息: ")
print(stats)

# 获取最大值和最小值的索引
max_index = series_with_index.idxmax()
min_index = series_with_index.idxmin()
print("最大值的索引: "+max_index)
print("最小值的索引: "+min_index)

# 四、基本运算 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# 算术运算
result = series * 2  # 所有元素乘以2
print("所有元素乘以2: ")
print(result)

# 过滤
filtered_series = series[series > 2]  # 选择大于2的元素
print("选择大于2的元素: ")
print(filtered_series)

# 数学函数
result = np.sqrt(series)  # 对每个元素取平方根
print("对每个元素取平方根: ")
print(result)